var express = require('express');
var router = express.Router();
var userService = require('../service/user.service');

var log4js = require('log4js');
var logger = log4js.getLogger();

/* GET users listing. */
router.post('/login', function(req, res, next) {
	var username = req.param('username');
	var password = req.param('password');

	if (username && password) {

		logger.info(username)
		logger.info(password)

		req.models.youa_bi_user_member.find({username: username, password: password.toUpperCase()}).all(function (err, result) {

			if (err || !result || result.length == 0) {
				res.json({
					result: '用户名或密码错误',
					error: -100
				});
			}else{
				var csrtToken = userService.login(username);
				res.json({
					result: {
						csrftoken: csrtToken,
						username: username
					},
					error: 0
				})
			}
		});
	} else {
		res.json({
			result: '却少用户名或密码',
			error: -100
		});
	}
});

router.post('/logout', function (req, res, next) {
	var username = req.params.username;

	userService.logout(username);
	res.json({
		result: true,
		error: 0
	});
});

router.post('/checkUserLogin', function (req, res, next) {
	var username = req.param('username');
	var csrtToken = req.param('csrftoken');

	var isLogin = userService.checkUserLogin(username, csrtToken);
	res.json({
		result: isLogin,
		error: 0
	})
})

module.exports = router;